package main;

import java.util.Random;

public class MatrixGenerator {
	
	/**
	 * Constructs the main matrix.
	 * Generates a vector, fills it and shuffles it.
	 * Fill matrix with the shuffled vector.
	 * @param size Indicates NxN Matrix
	 */
	public MatrixGenerator(int size) {
		this.size = size;
		//Generate vector with specified size and fill it.
		int[] vector = new int[(int) Math.pow(size, 2)]; //Create vector with the specified size
		for(int i = 1; i <= Math.pow(size, 2); i++)
			vector[i-1] = i;//Fill vector with sorted values
		shuffle(vector);//Shuffle vector
		matrix = matrix(vector);//Copy values of shuffled vector into matrix
		//RectangleGenerator rectangleGenerator = new RectangleGenerator(size);
	}
	
	/**
	 * Fisher Yates Shuffle
	 * @param vector with shuffled values
	 */
	private void shuffle(int[] vector) {
		Random random = new Random();
		for (int i = 0; i < vector.length; i++) {
			int j = i + random.nextInt(vector.length - i); //Random number from 0 <= j <= i
			//swap values;
			int temp = vector[i];
			vector[i] = vector[j];
			vector[j] = temp;	
		}
	}
	/**
	 * Copy shuffled vector into matrix
	 * @param vector 
	 * @return Matrix with shuffled values
	 */
	private int[][] matrix(int[] vector){
		int k = 0;
		int[][] matrix = new int[size][size];
		for(int i = 0; i < size; i++){
			for(int j = 0; j < size; j++){
				matrix[i][j] = vector[k];
				k++;
			}
		}
		return matrix;
	}
	
	/**
	 * 
	 * @return Matrix that will run the game
	 */
	public int[][] getMatrix() {
		return matrix;
	}
	
	private int size;
	private int[][] matrix;
}
